package com.atguigu.school.service.impl;

import com.atguigu.school.commons.MD5;
import com.atguigu.school.commons.Page;
import com.atguigu.school.mapper.AdminMapper;
import com.atguigu.school.pojo.Admin;
import com.atguigu.school.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    /**
     * 访问管理员数据
     */
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void saveOrUpdateAdmin(Admin admin) {
        if (admin.getId() == null || admin.getId() == 0) {
            this.adminMapper.insertAdmin(admin);
        } else {
            //需要更新密码
            if (admin.getPassword() != null && admin.getPassword().length() > 0) {
                admin.setPassword(MD5.encrypt(admin.getPassword()));
            }

            this.adminMapper.updateAdminById(admin);
        }
    }

    @Override
    public void deleteAdminsByIds(List<Integer> ids) {
        this.adminMapper.deleteAdminsByIds(ids);
    }

    @Override
    public Page<Admin> selectAdminsByName(String name, Integer current, Integer size) {
        Page<Admin> page = new Page<>(current, size, this.adminMapper.countAdminsByName(name));

        List<Admin> admins = this.adminMapper.selectAdminsByName(name, page.getOffset(), size);
        page.setRecords(admins);
        return page;
    }
}
